﻿Public Class GridAndFind
    Private dsBase As DataSet
    Private GridKey As String
    Private ToolHeader As ToolbarHeader
    Private FormTarget As Form
    Private cmsUse As Boolean
    Private rInd As Integer
    Private copyText As String
    Public Event GridCellEnter(sender As GridAndFind, GridKeyValue As String)
    Public Event GridDoubleClick(sender As GridAndFind)

    Public Sub Initialize(dataset As DataSet, GridPrimaryKey As String, Header As ToolbarHeader, CurrentForm As Form, cms As Boolean)
        rInd = -1
        dsBase = dataset
        GridKey = GridPrimaryKey
        ToolHeader = Header
        FormTarget = CurrentForm
        cmsUse = cms
        If (ToolHeader IsNot Nothing) Then
            ToolHeader.ActionCtrl(True, False, False, False, False, False, False, True, True)
        End If
        If (dsBase IsNot Nothing) Then
            If (dsBase.Tables(0).Rows.Count > 0) Then
                dgvBase.DataSource = dsBase.Tables(0)
                For i = 0 To dsBase.Tables(0).Columns.Count() - 1 Step +1
                    cbFind.Items.Add(dsBase.Tables(0).Columns(i).ToString())
                Next
                cbFind.SelectedIndex = 0
                If (ToolHeader IsNot Nothing) Then
                    ToolHeader.ActionCtrl(True, False, True, True, True, False, False, True, True)
                End If
                lblRowCount.Text = "Total Row : " + dgvBase.Rows.Count().ToString()
            End If
        End If

        With Me.dgvBase
            .RowsDefaultCellStyle.BackColor = Color.LightGray
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White
        End With
    End Sub

    Public Function getDataGridView() As DataGridView
        Return dgvBase
    End Function

    Private Sub dgvBase_Sorted(sender As Object, e As EventArgs) Handles dgvBase.Sorted
        If (dgvBase.Columns.Count > 0) Then
            cbFind.Text = dgvBase.SortedColumn.HeaderCell.Value
            txtFind.Focus()
        End If
    End Sub

    Private Sub cbFind_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbFind.SelectedIndexChanged
        Dim searchby As String = cbFind.Items.Item(cbFind.SelectedIndex)
        Dim dataType As String = dsBase.Tables(0).Columns(searchby).DataType.ToString()
        txtFind.Visible = True
        dtFind.Visible = False
        txtFind.Text = ""
        If (dataType = "System.DateTime") Then
            txtFind.Visible = False
            dtFind.Visible = True
        End If
    End Sub

    Private Sub dtFind_ValueChanged(sender As Object, e As EventArgs) Handles dtFind.ValueChanged
        txtFind.Text = dtFind.Value
    End Sub

    Private Sub txtFind_TextChanged(sender As Object, e As EventArgs) Handles txtFind.TextChanged
        If (cbFind.Items.Count() > 0) Then
            Dim searchby As String = cbFind.Items.Item(cbFind.SelectedIndex)
            Dim searchvalue As String = txtFind.Text.Trim()
            Dim dvFind As DataView = New DataView(dsBase.Tables(0))
            Dim dataType As String = dsBase.Tables(0).Columns(searchby).DataType.ToString()
            If (dataType = "System.String") Then
                dvFind.RowFilter = "[" & searchby & "] LIKE '%" & searchvalue & "%'"
            ElseIf (dataType = "System.DateTime") Then
                Dim searchDate As DateTime = dtFind.Value
                If (searchvalue = "" Or IsDate(searchvalue) = False) Then
                    dvFind.RowFilter = "[" & searchby & "] = [" & searchby & "]"
                Else
                    searchvalue = searchDate.ToString("yyyy-MM-dd")
                    dvFind.RowFilter = "[" & searchby & "] = '" & searchvalue & "'"
                End If
            ElseIf (dataType = "System.Decimal") Then
                If (txtFind.Text = "") Then
                    dvFind.RowFilter = "[" & searchby & "] = [" & searchby & "]"
                ElseIf (IsNumeric(txtFind.Text.Trim()) = False) Then
                    txtFind.Text = "0"
                    searchvalue = "0"
                Else
                    dvFind.RowFilter = "[" & searchby & "] >= " & searchvalue
                End If
            End If
            dgvBase.DataSource = dvFind
            lblRowCount.Text = "Total Row : " + dgvBase.Rows.Count().ToString()
        End If
    End Sub

    Private Sub dgvBase_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvBase.CellEnter
        rInd = e.RowIndex
        If (GridKey IsNot Nothing) Then
            Dim KeyValue As String = dgvBase.Rows(e.RowIndex).Cells(GridKey).Value.ToString()
            RaiseEvent GridCellEnter(Me, KeyValue)
        End If
    End Sub

    Private Sub dgvBase_DoubleClick(sender As Object, e As EventArgs) Handles dgvBase.DoubleClick
        RaiseEvent GridDoubleClick(Me)
    End Sub

    Private Sub dgvBase_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvBase.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
            If (GridKey IsNot Nothing) Then
                Dim KeyValue As String = dgvBase.Rows(e.RowIndex).Cells(GridKey).Value.ToString()
                RaiseEvent GridCellEnter(Me, KeyValue)
            End If
            ToolHeader.copyText = dgvBase.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            dgvBase.ClearSelection()
            dgvBase.CurrentCell = dgvBase.Rows(e.RowIndex).Cells(0)
            dgvBase.Rows(e.RowIndex).Selected = True
            'dgvBase.ContextMenuStrip = cmsBase
        End If
    End Sub

    Private Sub txtFind_KeyDown(sender As Object, e As KeyEventArgs) Handles txtFind.KeyDown
        If (dgvBase.Rows.Count > 0) Then
            If (e.KeyCode = Keys.Enter) Then
                RaiseEvent GridDoubleClick(Me)
            Else
                If (rInd > -1) Then
                    If (e.KeyCode = Keys.Down) Then
                        If (rInd < dgvBase.Rows.Count - 1) Then
                            rInd += 1
                        End If
                    End If
                    If (e.KeyCode = Keys.Up) Then
                        If (rInd > 0) Then
                            rInd -= 1
                        End If
                    End If
                    dgvBase.ClearSelection()
                    dgvBase.Rows(rInd).Selected = True
                End If
            End If
        End If
    End Sub

    Private Sub dgvBase_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            RaiseEvent GridDoubleClick(Me)
        End If
    End Sub
End Class
